package top.roadvast.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;

import java.io.InputStream;
import java.util.zip.ZipInputStream;

/**
 * @Author 郭泽鹏
 * @Date 2020/6/23 0:04
 * @Version 1.0
 * 流程定义的部署
 * 流程数据添加到哪些表？
 * act_re_deployment：部署信息
 * act_re_procdef：流程定义的一些信息
 * act_ge_bytearray：流程定义的bpmn文件及png文件
 */
public class ActivitiDeployment {

    @Test
    public void pngTest() {
        //1、创建ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //3、进行流程定义的部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")   //添加bpmn资源
                .addClasspathResource("diagram/holiday.png")    //定义png资源
                .name("请假申请单流程")
                .deploy();

        //4、输出部署的一些信息
        System.out.println(deployment.getName());        //流程定义部署名称
        System.out.println(deployment.getId());      //流程定义部署id
        System.out.println(deployment.getDeploymentTime());     //流程定义部署时间
    }

    @Test
    public void zipTest() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagram/holiday-group.zip");
        ZipInputStream zipInputStream = new ZipInputStream(in);
        //1、创建ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2、得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //3、进行流程定义的部署
        Deployment deployment = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)      //指定zip格式的文件完成部署
                .name("请假申请单流程-variable")        //部署名称
                .deploy();      //完成部署

        //4、输出部署的一些信息
        System.out.println("流程定义部署名称：" + deployment.getName());        //流程定义部署名称
        System.out.println("流程定义部署ID：" + deployment.getId());      //流程定义部署id
        System.out.println("流程定义部署时间：" + deployment.getDeploymentTime());     //流程定义部署时间
        //获取流程定义
        final ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        System.out.println("流程定义ID：" + processDefinition.getId());
    }
}